#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(300,300,WINDOW_WIDTH,WINDOW_HEIGHT);
    map=new Astar("Map",this,10,5);
    setMenuBar();
    setToolBar();
}

MainWindow::~MainWindow()
{

}
void MainWindow::setMenuBar(){
    QMenu *fileMenu=menuBar()->addMenu("文件");
    QMenu *settingMenu=menuBar()->addMenu("Settings");
    QMenu *aboutMenu=menuBar()->addMenu("Other");
    QAction *setxyAction=new QAction("设置地图大小");
    QAction *fileopenAction=new QAction("Open File");
    QAction *filesaveAction=new QAction("Save As...");
    QAction *aboutAction=new QAction("About");

    fileMenu->addAction(fileopenAction);
    fileMenu->addAction(filesaveAction);
    fileMenu->addSeparator();


    aboutMenu->addAction(aboutAction);
    aboutMenu->addSeparator();

    settingMenu->addAction(setxyAction);
    settingMenu->addSeparator();

    connect(setxyAction,SIGNAL(triggered(bool)),this,SLOT(openxyDialog()));

}
void MainWindow::openxyDialog(){
    SetxyDialog *aa=new SetxyDialog(this);
    //qDebug()<<map;
    if(aa->exec()==QDialog::Accepted){
        delete map;
        map=new Astar("Map",this,aa->x,aa->y);
        setpainter();
        map->show();
        //qDebug()<<map;
    }
    delete aa;
    return;
}
void MainWindow::clearmap(){
    delete map;
    map=new Astar("Map",this,map->w,map->h);
    map->show();
    setpainter();
    //qDebug()<<"clear";
}
void MainWindow::setToolBar(){
    QToolBar *barset=addToolBar("设定");
    QAction *clearAction=new QAction("清空");
    barset->addAction(clearAction);
    connect(clearAction,SIGNAL(triggered(bool)),this,SLOT(clearmap()));
    startbtn=new QRadioButton(this);
    startbtn->setText("设置起点");
    endbtn=new QRadioButton(this);
    endbtn->setText("设置终点");
    blocksbtn=new QRadioButton(this);
    blocksbtn->setText("设置障碍");
    barset->addWidget(startbtn);
    barset->addWidget(endbtn);
    barset->addWidget(blocksbtn);
    connect(startbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));
    connect(endbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));
    connect(blocksbtn,SIGNAL(clicked(bool)),this,SLOT(setpainter()));

    startbtn->click();

    QAction *startAstar=new QAction("run");
    barset->addAction(startAstar);
    connect(startAstar,SIGNAL(triggered(bool)),this,SLOT(startA()));

    QAction *clearways=new QAction("清除路径");
    barset->addAction(clearways);
    connect(clearways,SIGNAL(triggered(bool)),this,SLOT(clearways()));
}
void MainWindow::setpainter(){
    if(startbtn->isChecked()) map->setpainterstatus(2);
    if(endbtn->isChecked()) map->setpainterstatus(3);
    if(blocksbtn->isChecked()) map->setpainterstatus(0);
}
void MainWindow::startA(){
    map->runAstar();
}
void MainWindow::clearways(){
    map->clearways();

}

